package drds.binlog.event_filter.aviater;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;
import drds.binlog.common.Authors;
import drds.binlog.event_filter.EventFilter;
import drds.common.$;
import drds.common.Author;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 * 基于aviater进行tableName正则匹配的过滤算法
 */
@Author(name = Authors.LI_YANG)
public class RegexFilter implements EventFilter<String>
{
    private static final RegexFunction regexFunction = new RegexFunction();
    private static final Comparator<String> stringComparator = new StringComparator();

    static
    {
        AviatorEvaluator.addFunction(regexFunction);
    }

    private final Expression expression = AviatorEvaluator.compile("regex(pattern,target)", true);
    @Setter
    @Getter
    final private String pattern;
    @Setter
    @Getter
    final private boolean defaultEmptyValue;

    public RegexFilter(String pattern)
    {
        this(pattern, true);
    }

    public RegexFilter(String pattern, boolean defaultEmptyValue)
    {
        this.defaultEmptyValue = defaultEmptyValue;
        List<String> list = null;
        if ($.isNullOrEmpty(pattern))
        {
            list = new ArrayList<String>();
        } else
        {
            String[] ss = StringUtils.split(pattern, ",");
            list = Arrays.asList(ss);
        }
        Collections.sort(list, stringComparator);
        // 对pattern进行头尾完全匹配
        list = completionPatternStringList(list);
        this.pattern = StringUtils.join(list, "|");
    }

    public boolean match(String string)
    {
        if ($.isNullOrEmpty(pattern))
        {
            return defaultEmptyValue;
        }

        if ($.isNullOrEmpty(string))
        {
            return defaultEmptyValue;
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pattern", pattern);
        map.put("target", string);
        return (Boolean) expression.execute(map);
    }

    /**
     * 正则表达式进行头尾完全匹配
     */
    private List<String> completionPatternStringList(List<String> patternStringList)
    {
        List<String> stringList = new ArrayList<String>();
        for (String patternString : patternStringList)
        {
            StringBuilder sb = new StringBuilder();
            sb.append("^");
            sb.append(patternString);
            sb.append("$");
            stringList.add(sb.toString());
        }
        return stringList;
    }

    /**
     * 对正则表达式进行了从长到短的排序
     */
    private static class StringComparator implements Comparator<String>
    {

        @Override
        public int compare(String string1, String string2)
        {
            if (string1.length() > string2.length())
            {
                return -1;
            } else if (string1.length() < string2.length())
            {
                return 1;
            } else
            {
                return 0;
            }
        }
    }

}
